package com.gscitysfy.cus.drain.modules.stat.service.impl;

import com.gscitysfy.cus.drain.modules.stat.dto.DrainDispatchDangerInfoDto;
import com.gscitysfy.cus.drain.modules.stat.dto.DrainDispatchDangerStatInfoDto;
import com.gscitysfy.cus.drain.modules.stat.dto.DrainRescueTimeStatInfoDto;
import com.gscitysfy.cus.drain.modules.stat.dto.DrainRescueTimeStatTaskInfoDto;
import com.gscitysfy.cus.drain.modules.stat.mapper.DrainRescueTimeStatMapper;
import com.gscitysfy.cus.drain.modules.stat.service.IDrainRescueTimeStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;
import java.util.OptionalDouble;
import java.util.stream.Collectors;

/**
 * @BelongsProject: drain_cus_server_huaibei
 * @BelongsPackage: com.gscitysfy.cus.drain.modules.stat.service.impl
 * @Author: shenglin
 * @CreateTime: 2022-11-17  14:16
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class IDrainRescueTimeStatServiceImpl implements IDrainRescueTimeStatService {

    @Autowired
    private DrainRescueTimeStatMapper drainRescueTimeStatMapper;

    @Override
    public DrainRescueTimeStatInfoDto getStatInfo(String st, String et, String name) {
        List<DrainRescueTimeStatTaskInfoDto> rescueTimeStatTaskInfo = drainRescueTimeStatMapper.getRescueTimeStatTaskInfo(st, et, name);
        //获取任务总时长
        long timeSum = rescueTimeStatTaskInfo.stream().mapToLong((e->e.getTimer()!=null?e.getTimer():0L)).sum();
        String timeSumResult = secondsToTime(timeSum);
        OptionalDouble average = rescueTimeStatTaskInfo.stream().mapToLong((e -> e.getTimer() != null ? e.getTimer() : 0L)).average();
        //平均单次任务时长转换为时分秒单位
        String timeSumAvg="0s";
        if(average.isPresent()){
            timeSumAvg= secondsToTime(Math.round(average.getAsDouble()));
        }
        //将单个任务的时长转换为时分秒单位
        rescueTimeStatTaskInfo.stream().forEach(e->{
            e.setTaskTime(secondsToTime(e.getTimer()));
        });
        return new DrainRescueTimeStatInfoDto(rescueTimeStatTaskInfo.size(),timeSumResult,timeSumAvg,rescueTimeStatTaskInfo);
    }

    @Override
    public List<DrainDispatchDangerStatInfoDto> getDispatchDangerStatInfo(String st, String et) {
        List<DrainDispatchDangerStatInfoDto> dispatchDangerStatInfo = drainRescueTimeStatMapper.getDispatchDangerStatInfo(st, et);
        List<DrainDispatchDangerInfoDto> dispatchDangerInfo = drainRescueTimeStatMapper.getDispatchDangerInfo(st, et);
        for (DrainDispatchDangerStatInfoDto drainDispatchDangerStatInfoDto : dispatchDangerStatInfo) {
            drainDispatchDangerStatInfoDto.setDangerInfoDtoList(dispatchDangerInfo.stream().filter(s -> s.getZoneId().equals(drainDispatchDangerStatInfoDto.getZoneId())).collect(Collectors.toList()));
        }
        return dispatchDangerStatInfo;
    }

    @Override
    public List<DrainDispatchDangerStatInfoDto> getTaskStatInfo(String st, String et) {
        List<DrainDispatchDangerStatInfoDto> taskStatInfo = drainRescueTimeStatMapper.getTaskStatInfo(st, et);
        List<DrainDispatchDangerInfoDto> taskInfo = drainRescueTimeStatMapper.getTaskInfo(st, et);
        for (DrainDispatchDangerStatInfoDto drainDispatchDangerStatInfoDto : taskStatInfo) {
            drainDispatchDangerStatInfoDto.setDangerInfoDtoList(taskInfo.stream().filter(s -> s.getZoneId().equals(drainDispatchDangerStatInfoDto.getZoneId())).collect(Collectors.toList()));
        }
        return taskStatInfo;
    }

    //根据秒数转换为时分秒方法
    private String secondsToTime(Long seconds){
        if(seconds==null){
            return "0s";
        }
        int h= Math.toIntExact(seconds / 3600);
        int m= Math.toIntExact(seconds % 3600)/60;
        int s= Math.toIntExact(seconds % 3600)%60;
        if(h>0){
            return h+"h"+m+"min"+s+"s";
        }
        if(m>0){
            return m+"min"+s+"s";
        }
        return s+"s";
    }
}
